/*
 * Copyright (C) 2012 The Android Open Source Project
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.happen.android.thrid.icache.ui;

import org.happen.android.base.BaseV4Fragment;
import org.happen.android.thrid.icache.ICaches;
import org.happen.android.thrid.icache.cache.ImageWorker;
import org.happen.android.thrid.icache.ui.ImageDetailActivity.ExtraParams;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import org.happen.android.R;

/**
 * The main fragment that powers the ImageGridActivity screen. Fairly straight
 * forward GridView implementation with the key addition being the ImageWorker
 * class w/ImageCache to load children asynchronously, keeping the UI nice and
 * smooth and caching thumbnails for quick retrieval. The cache is retained over
 * configuration changes like orientation change so the images are populated
 * quickly as the user rotates the device.
 */
public class ImageGridFragment extends BaseV4Fragment
{
	private static final String TAG = "ImageGridFragment";

	ImageAdapter mAdapter;
	ImageWorker mImageWorker;

	protected int getLayoutId()
	{
		return R.layout.thrid_icache_image_grid_fragment;
	}

	protected void init(Bundle savedInstanceState)
	{
		mAdapter = new ImageAdapter(getActivity(), ICaches.tempImageUrls);
		mImageWorker = ICaches.newImageWorker(getActivity(), TAG);

		GridView mGridView = (GridView) contentView.findViewById(R.id.gridView);
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(mAdapter);
	}

	public void onResume()
	{
		super.onResume();
		mImageWorker.setOnScreen(TAG, true);
		mAdapter.notifyDataSetChanged();
	}

	public void onPause()
	{
		super.onPause();
		mImageWorker.setOnScreen(TAG, false);
	}

	/**
	 * The main adapter that backs the GridView. This is fairly standard except
	 * the number of columns in the GridView is used to create a fake top row of
	 * empty views as we use a transparent ActionBar and don't want the real top
	 * row of images to start off covered by it.
	 */
	private class ImageAdapter extends BaseAdapter implements AdapterView.OnItemClickListener
	{

		private final Activity mContext;
		private GridView.LayoutParams mImageViewLayoutParams;
		String[] imageUrls;

		public ImageAdapter(Activity context, String[] imageUrls)
		{
			super();
			mContext = context;
			this.imageUrls = imageUrls;
			int width = (int) ((context.getResources().getDisplayMetrics().widthPixels / 3) * 1.5);
			mImageViewLayoutParams = new GridView.LayoutParams(LayoutParams.MATCH_PARENT, width);
		}

		public int getCount()
		{
			return imageUrls.length;
		}

		public Object getItem(int position)
		{
			return imageUrls[position];
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup container)
		{
			ImageView imageView;
			if (convertView == null)
			{
				imageView = new ImageView(mContext);
				imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				imageView.setLayoutParams(mImageViewLayoutParams);
			} else
			{
				imageView = (ImageView) convertView;
			}

			mImageWorker.loadBitmap(imageUrls[position], imageView);
			return imageView;
		}

		public void onItemClick(AdapterView<?> parent, View v, int position, long id)
		{
			ExtraParams params = new ExtraParams();
			params = new ExtraParams();
			params.imageUrls = imageUrls;
			params.extraCurrentItem = position;

			ICaches.gotoImageDetail(mContext, params);
		}
	}
}